package com.silence.gmall.config;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.aop.ObservedAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author silence
 * @Email 792245022@qq.com
 * @Date 2025-06-11 16:39
 * @Description
 */
@Slf4j
@Configuration
public class TracingConfig {

    /**
     * 支持@Observed注解
     */
    @Bean
    ObservedAspect observedAspect(ObservationRegistry observationRegistry) {
        return new ObservedAspect(observationRegistry);
    }

    // 可选：自定义观察处理器
    @Bean
    public ObservationHandler<Observation.Context> customHandler() {
        return new ObservationHandler<>() {
            @Override
            public void onStart(Observation.Context context) {
                log.info("观察开始: {}", context.getName());
            }

            @Override
            public void onStop(Observation.Context context) {
                log.info("观察结束: {}", context.getName());
            }

            @Override
            public boolean supportsContext(Observation.Context context) {
                return true;
            }
        };
    }

}
